#include "shadewidget.h"
//#include "ui_shadewidget.h"
Shadewidget::Shadewidget(QMainWindow *parent) :
    parent(parent)
{
//    ui->setupUi(this);
//    centralWidget = new QWidget(this);
//    // 设置中心部件
//    setCentralWidget(centralWidget);
    QRect screenGeometry = QApplication::desktop()->screenGeometry();
    int screenWidth = screenGeometry.width();
    int screenHeight = screenGeometry.height();
//    qDebug()<<screenWidth<<" "<<screenHeight<<Qt::endl;
    this->resize(screenWidth,screenHeight);
//    this->resize(1800,1600);
    QPalette palette=this->palette();
    palette.setBrush(QPalette::Background,QColor(0,0,0));
    this->setPalette(palette);
    this->setWindowOpacity(0.4);//设置窗口透明度
    this->setWindowFlags(Qt::FramelessWindowHint|windowFlags());//去掉标题栏
    this->setWindowTitle("screen");

    menu = new QMenu(this);//创建右键菜单
    menu->addAction("复制选中到粘贴板", this, [=](){copy(true);});
    menu->addAction("选中图像另存为", this, [=](){save();});
    menu->addAction("复制全屏到粘贴板", this, [=](){
        x=0;y=0;w=QApplication::desktop()->width();h=QApplication::desktop()->height();
        copy(true);
    });
    menu->addAction("全屏图像另存为", this, [=](){
        x=0;y=0;w=QApplication::desktop()->width();h=QApplication::desktop()->height();
        save();
    });
    menu->addAction("退出截图", this, [=](){
        this->setGeometry(0, 0, 1,1);    //遮罩窗口位置
        x=y=w=h=0;//关闭之后清空
        update();
        this->hide();
        parent->show();
    });
    menu->setStyleSheet("background-color: rgb(30, 30, 30, 100);color: rgb(255, 255, 255);"
                        "border-right:2px solid #aaaaaa; "
                        "border-bottom:2px solid #aaaaaa;"
                        "border-left:2px solid #aaaaaa;"
                        "border-top:2px solid #aaaaaa; "
                        "border-radius:5px;"
                        "font: 16pt ""黑体"";"
                        "selection-background-color: rgb(100, 40, 40);");

}

Shadewidget::~Shadewidget()
{
    delete menu;
    delete centralWidget;
}

void Shadewidget::closeEvent(QCloseEvent *event)
{
    event->ignore();
}

//实现窗口移动
void Shadewidget::mouseMoveEvent(QMouseEvent *event)
{
    end_point=event->globalPos()-this->pos();//获取点击的坐标
    if(end_point.x()>start_point.x()){
        x=start_point.x();//比较起始坐标
        w=end_point.x()-start_point.x();
    }
    else {
        x=end_point.x();
        w=start_point.x()-end_point.x();
    }
    if(end_point.y()>start_point.y()){
        y=start_point.y();//比较起始坐标
        h=end_point.y()-start_point.y();
    }
    else {
        y=end_point.y();
        h=start_point.y()-end_point.y();
    }
    update();
//    qDebug()<<start_point<<end_point<<event->globalPos()-this->pos();//获取点击的坐标
}

//鼠标右击事件
void Shadewidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::RightButton){//右键弹出菜单
        menu->exec(cursor().pos());//菜单显示的位置跟随鼠标
    }
    if(event->x()>x&&event->x()<(x+w)&&event->y()>y&&event->y()<(y+h)){//鼠标点击在 画完范围内 确定截图
        copy(true);//将截图复制到粘贴板
    }
    else{
        x=y=w=h=0;
        update();
        start_point=event->globalPos()-this->pos();//获取点击的坐标
    }
}

//鼠标右击释放事件
void Shadewidget::mouseReleaseEvent(QMouseEvent *event)
{
//    this->hide();
//    parent->show();
}

// 键盘按下事件
void Shadewidget::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape){ // 是否按下Esc键
        this->setGeometry(0, 0, 1,1);    //遮罩窗口位置
        x=y=w=h=0;//关闭之后清空
        update();
        this->hide();
        parent->show();
    }
    else QWidget::keyPressEvent(event);
}

//绘制
void Shadewidget::paintEvent(QPaintEvent */*event*/)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.save();
    painter.setPen(Qt::green);
    QPointF topLeftPot(x, y);
    QPointF bottomRightPot(x+w, y+h);//右上角
    QRectF barRect = QRectF(topLeftPot, bottomRightPot);
    painter.setBrush(Qt::white);
    painter.drawRect(barRect);
    painter.restore();
}

//将截图复制到粘贴板
void Shadewidget::copy(bool hideflag)
{
    //截屏
    this->setWindowOpacity(0);//设置窗口透明度
    screen=QPixmap::grabWindow(QApplication::desktop()->winId(),x,y,w,h);
    this->setWindowOpacity(0.4);//设置窗口透明度
    //保存到粘贴板
    QClipboard *pic=QApplication::clipboard();
    pic->setPixmap(screen);
    this->setGeometry(0, 0, 1,1);    //遮罩窗口位置
    //关闭之后清空
    x=y=w=h=0;
    update();
    if(hideflag)
        this->hide();
        parent->show();
}

//截图另存为
void Shadewidget::save()
{
    copy(false);//将截图复制到粘贴板
    QString savename=QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)+"/截图.png";//默认路径：桌面  //默认格式：png
    QString fileName = QFileDialog::getSaveFileName(this,"截图另存为",savename,"PNG (*.png);;BMP (*.bmp);;JPEG (*.jpg *.jpeg)");
    if (!fileName.isEmpty()){
        screen.save(fileName);
    }
    screen.save(fileName);
    this->hide();
    parent->show();
}
